local ClientOHOH = _G.ClientOHOH;
local TweenService = game:GetService("TweenService")
local M = ClientOHOH.class.new(script,ClientOHOH.files.OHOHOLYBUIBase());
local CallBackManager = ClientOHOH.load.CallBackManager();
local initBackColor = nil
local TweenService = game:GetService("TweenService")
local leftConnect = nil
local rightConnect = nil
local white = Color3.fromRGB(255,255, 255)
local black = Color3.fromRGB(31, 26, 26)
local colorWhiteSequence = ColorSequence.new{
    ColorSequenceKeypoint.new(0,white),
    ColorSequenceKeypoint.new(1,white),
}

local colorBlackSequence = ColorSequence.new{
    ColorSequenceKeypoint.new(0,black),
    ColorSequenceKeypoint.new(1,black),
}

function InitInsts(ui)
ui.insts = {};
ui.insts.rollBtn = "ctrl.countdown.btn"
ui.insts.rollNum = "ctrl.countdown.num"
ui.insts.rollLeft = "ctrl.countdown.left"
ui.insts.rollRight = "ctrl.countdown.right"

ui.insts.autoBtn = "ctrl.auto.btn"
ui.insts.autoSelected = "ctrl.auto.selected"
ui.insts.arrow = "ctrl.auto.arrow"

ui.insts.fastBtn = "ctrl.fast.btn"
ui.insts.fastSelected = "ctrl.fast.selected"

ui.insts.drop = "ctrl.drop"

end

function M.new(...)
    return M(...);
end

function M.ctor(self)
    InitInsts(self)
    M.super.ctor(self,ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.RollGui"))
    self:Init()
    self.lastLeftConnect = nil
    self.lastRightConnect = nil
end

function M.Init(self)
    self.AutoISActivated = false
    self.FastIsActivated = false
    initBackColor = self.insts.rollBtn.BackgroundColor3
end

--------------Roll------------
--roll点击效果，圆形进度条
function M.RollClick(self,cd)
    local tweenInfo = TweenInfo.new(cd/2,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut)
    local tweenRotation = {
        Rotation = 180
    }
    if rightConnect then
        rightConnect:Disconnect()
        self.insts.rollLeft.Color = colorWhiteSequence
    end
    if leftConnect then
        leftConnect:Disconnect()
    end
    self.insts.rollLeft.Color = colorBlackSequence
    
    self.insts.rollRight.Rotation = 0
    self.insts.rollLeft.Rotation = -0.1
    local tweenRight = TweenService:Create(self.insts.rollRight,tweenInfo,tweenRotation)
    local tweenLeft = TweenService:Create(self.insts.rollLeft,tweenInfo,tweenRotation)
    leftConnect = tweenLeft.Completed:Connect(function()
        self.insts.rollRight.Rotation = 180
        self.insts.rollLeft.Rotation = 180
    end)
    rightConnect = tweenRight.Completed:Connect(function()
        self.insts.rollLeft.Color = colorWhiteSequence
        tweenLeft:Play()
    end)
    tweenRight:Play()
end

--创建显示抽出道具的cell
function M.CreateCell(self,carId,database,cd)
    local cell = ClientOHOH.load.PartCell().new(carId,self.insts.drop,database)
    cell:SetTitleVisibility(true)
    cell:SetPartWeightVisibility(true)
    cell:SetPartInVisibility(true)
    self:AddProcess(function()
        self:ShowCell(cell,cd)
        self:Wait(cd*1.3)
        self:HideCell(cell,cd)
        self:Wait(cd*0.4)
        cell.insts.btn.Visible = false
        self:Wait(cd*0.3)
        cell:dtor()
    end)
    
end

--cell显示的动画效果,tweenSize
function M.ShowCell(self,cell,cd)
    local originSize = cell.insts.btn.Size
    cell.insts.btn.Size = UDim2.new(originSize.X.Scale*0.9,0,originSize.Y.Scale*0.9,0)
    local tweenInfo = TweenInfo.new(
        cd*0.2,
        Enum.EasingStyle.Quart,
        Enum.EasingDirection.Out,
        0,
        false,
        0
    )
    local goal = {
        Size = originSize
    }
    local tween = TweenService:Create(cell.insts.btn,tweenInfo,goal)

    tween:Play();
end

--cell隐藏的动画效果,tweenTransparency
function M.HideCell(self,cell,cd)

    local tweenInfo = TweenInfo.new(
        cd*0.7,
        Enum.EasingStyle.Quart,
        Enum.EasingDirection.InOut,
        0,
        false,
        0
    )
    local goal = {
        Size = UDim2.new(0,0,0,0)
    }
    local tween = TweenService:Create(cell.insts.btn,tweenInfo,goal)

    tween:Play();
end
------------AutoRoll----------
local lastPlay = nil
local ti = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.In, -1, false)

--自动roll点击效果，背景颜色修改，箭头旋转
function M.AutoRollClick(self,isActivated)
    if isActivated then
        local tw = TweenService:Create(self.insts.arrow, ti, {Rotation =self.insts.arrow.Rotation+360})
        lastPlay = tw
        self.insts.autoBtn.BackgroundColor3 = self.insts.autoSelected
        tw:Play()
    else
        self.insts.autoBtn.BackgroundColor3 = initBackColor
        if lastPlay then
            lastPlay:Pause()
        end
    end
end

------------FastRoll---------
--快速Roll点击效果，背景颜色修改
function M.FastRollClick(self,isActivated)
    if isActivated then
        self.insts.fastBtn.BackgroundColor3 = self.insts.fastSelected
    else
        self.insts.fastBtn.BackgroundColor3 = initBackColor
    end
end


function M.dtor(self)
    M.super.dtor(self);
end

return M